package com.lzx.iteam.provider;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.lzx.iteam.bean.AttendanceCalendarMsgData;
import com.lzx.iteam.bean.LegworkRecordData;
import com.lzx.iteam.util.PreferenceUtil;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Megan on 16/4/19.
 */
public class AttendanceCalendarDB {

    private static final String DB_NAME = "attendanceCalendar.db";
    private SQLiteDatabase mDb;

    private static final String TYPE = "type";//动作类型，1.打卡，2.审批
    private static final String ID = "a_id";//动作ID
    private static final String DAY = "day";
    private static final String DATE = "date";
    private static final String USER_ID = "user_id";
    private static final String FORM_ID = "form_id";//考勤表id
    private static final String LATITUDE = "latitude";//纬度
    private static final String LONGITUDE = "longitude";//经度
    private static final String PART = "part";//时段，1.上午(签到)，2.下午(签退)
    private static final String STATUS = "status";//状态，1.迟到，6.正常,2.早退
    private static final String TIME = "time";//打卡时间戳
    private static final String CHEAT = "cheat";//是否打卡作弊
    private static final String CHEAT_DEVICE_ID = "cheat_device_id";
    private static final String ATTEN_CALENDAR_ID = "atten_calendar_id";
    private static final String GROUP_ID = "group_id";
    private static final String APPROVAL_END_PART = "approval_end_part";//时间段，1.上午，2.下午
    private static final String APPROVAL_STATUS = "approval_status";//状态 1.通过，0.等待，2.未通过
    private static final String APPROVAL_REASON = "approval_reason";//理由
    private static final String APPROVAL_TYPE = "approval_type";//申请类型, 1.出差，2.请假，3.外出，4.签到
    private static final String APPROVAL_DONE_TIME = "approval_done_time";//审批处理的时间戳
    private static final String APPROVAL_ADDRESS = "approval_address";//地址
    private static final String APPROVAL_LEAVE_TYPE = "approval_leave_type";//请假类型 0.事假，1.病假，2.年假，3.婚假，4.产假
    private static final String APPROVAL_END_TIME = "approval_end_time";
    private static final String APPROVAL_ADMIN = "approve_admin";//审批人
    private static final String APPROVAL_DAY = "approval_day";//出差天数,最少0.5天(半天)
    private static final String APPROVAL_START_TIME = "approval_start_time";
    private static final String APPROVAL_START_PART = "approval_start_part";////时间段，1.上午，2.下午
    private static final String APPROVAL_TIME = "approval_time";//创建时间戳

    private static final String ADDRESS = "address";
    private static final String REMARK = "remark";
    private static final String ATTACHMENT = "attachment";


    public AttendanceCalendarDB(Context context) {
        PreferenceUtil mPreferenceUtil = PreferenceUtil.getInstance(context);
        mDb = context.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);

    }

    /**
     * 创建一张表名为年-月-日的日常表
     *
     * @param date 日期 格式为yyyy_MM_dd
     */
    private void createScheduleTable(String date) {
        mDb.execSQL("CREATE table IF NOT EXISTS _" + date
              + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
              + ID + " TEXT, "
              + TYPE + " TEXT, "
              + USER_ID + " TEXT, "
              + DAY + " TEXT, "
              + DATE + " TEXT, "
              + FORM_ID + " TEXT, "
              + LATITUDE + " TEXT, "
              + LONGITUDE + " TEXT, "
              + PART + " TEXT, "
              + STATUS + " TEXT, "
              + TIME + " TEXT, "
              + CHEAT + " TEXT, "
              + CHEAT_DEVICE_ID + " TEXT, "
              + ATTEN_CALENDAR_ID + " TEXT, "
              + GROUP_ID + " TEXT, "
              + APPROVAL_END_PART + " TEXT, "
              + APPROVAL_STATUS + " TEXT, "
              + APPROVAL_REASON + " TEXT, "
              + APPROVAL_TYPE + " TEXT, "
              + APPROVAL_DONE_TIME + " TEXT, "
              + APPROVAL_ADDRESS + " TEXT, "
              + APPROVAL_LEAVE_TYPE + " TEXT, "
              + APPROVAL_END_TIME + " TEXT, "
              + APPROVAL_ADMIN + " TEXT, "
              + APPROVAL_DAY + " TEXT, "
              + APPROVAL_TIME + " TEXT, "
              + APPROVAL_START_TIME + " TEXT, "
              + APPROVAL_START_PART + " TEXT, "
              + ADDRESS + " TEXT, "
              + ATTACHMENT + " TEXT, "
              + REMARK + " TEXT ); "

        );
    }

    /**
     * 添加
     */
    public synchronized void addAttendanceCalendar(String type, String date, String day, Object object) {
        createScheduleTable(date);
        if ("0".equals(type)) {
            AttendanceCalendarMsgData attendanceCalendarMsgData = (AttendanceCalendarMsgData) object;
            mDb.execSQL(
                  "insert into _" + date + " (" + ID + "," + TYPE + "," + DAY + "," + DATE + "," + USER_ID + "," + FORM_ID + "," + LATITUDE + "," + LONGITUDE + "," + PART + "," + STATUS + "," + TIME + "," + CHEAT + "," + CHEAT_DEVICE_ID + "," + GROUP_ID + ","+ATTEN_CALENDAR_ID+"," + APPROVAL_END_PART + "," + APPROVAL_STATUS + "," + APPROVAL_REASON + "," + APPROVAL_TYPE + "," + APPROVAL_DONE_TIME + "," + APPROVAL_ADDRESS + "," + APPROVAL_LEAVE_TYPE + "," + APPROVAL_END_TIME + "," + APPROVAL_ADMIN + "," + APPROVAL_DAY + "," + APPROVAL_START_TIME + "," + APPROVAL_START_PART + "," + APPROVAL_TIME
                        + ") values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                  new Object[]{attendanceCalendarMsgData.get_id(), attendanceCalendarMsgData.getType(), day, attendanceCalendarMsgData.getDate(), attendanceCalendarMsgData.getUser_id(), attendanceCalendarMsgData.getForm_id(), attendanceCalendarMsgData.getLatitude(), attendanceCalendarMsgData.getLongitude(), attendanceCalendarMsgData.getPart(), attendanceCalendarMsgData.getStatus(), attendanceCalendarMsgData.getTime(), attendanceCalendarMsgData.getCheat(), attendanceCalendarMsgData.getCheat_device_id(),
                        attendanceCalendarMsgData.getGroup_id(), attendanceCalendarMsgData.getAtten_calendar_id(),attendanceCalendarMsgData.getApproval_end_part(), attendanceCalendarMsgData.getApproval_status(), attendanceCalendarMsgData.getApproval_reason(), attendanceCalendarMsgData.getApproval_type(), attendanceCalendarMsgData.getApproval_done_time(), attendanceCalendarMsgData.getApproval_address(), attendanceCalendarMsgData.getApproval_leave_type(), attendanceCalendarMsgData.getApproval_end_time(), attendanceCalendarMsgData.getApprove_admin(), attendanceCalendarMsgData.getApproval_day(), attendanceCalendarMsgData.getApproval_start_time(), attendanceCalendarMsgData.getApproval_start_part(), attendanceCalendarMsgData.getApproval_time()});

        } else {//外勤
            LegworkRecordData legworkRecordData = (LegworkRecordData) object;
            mDb.execSQL(
                  "insert into _" + date + " (" + ID + "," + TYPE + "," + DAY + "," + USER_ID + "," + FORM_ID + "," + LATITUDE + "," + LONGITUDE + "," + GROUP_ID + "," + ADDRESS + "," + REMARK + "," + TIME + "," + ATTACHMENT
                        + ") values(?,?,?,?,?,?,?,?,?,?,?,?)",
                  new Object[]{legworkRecordData.getId(), "-1", day, legworkRecordData.getUserId(), legworkRecordData.getFormId(), legworkRecordData.getLatitude(), legworkRecordData.getLongitude(), legworkRecordData.getGroupId(), legworkRecordData.getAddress(), legworkRecordData.getRemark(), legworkRecordData.getTime(), legworkRecordData.getAttachment()});

        }


    }

    public synchronized void addAttendanceCalendar(){

    }

    /**
     * 查询某天的考勤
     *
     * @return
     */
    public synchronized ArrayList<Object> findAttendanceByDate(String date, String day, String uid, String groupId) {
        createScheduleTable(date);
        ArrayList<Object> datas = new ArrayList<Object>();
      /*  int start = (currentPage - 1) * pageSize;
        int end = start + pageSize;*/
        /*String sql = "select * from _" + eventID + " order by _id  desc limit  "
                + start + " , " + end;*/
        String sql = "select * from _" + date + " where user_id = " + uid + " and day = " + day + " and group_id = " + groupId + " order by _id  asc ";
        Cursor cursor = null;
        try {
            cursor = mDb.rawQuery(sql, null);
            while (cursor.moveToNext()) {
                if ("1".equals(cursor.getString(cursor.getColumnIndex(TYPE))) || "2".equals(cursor.getString(cursor.getColumnIndex(TYPE)))) {
                    AttendanceCalendarMsgData attendanceCalendarMsgData = new AttendanceCalendarMsgData();
                    attendanceCalendarMsgData.set_id(cursor.getString(cursor.getColumnIndex(ID)));
                    attendanceCalendarMsgData.setType(cursor.getString(cursor.getColumnIndex(TYPE)));
                    attendanceCalendarMsgData.setForm_id(cursor.getString(cursor.getColumnIndex(FORM_ID)));
                    attendanceCalendarMsgData.setUser_id(cursor.getString(cursor.getColumnIndex(USER_ID)));
                    attendanceCalendarMsgData.setLatitude(cursor.getString(cursor.getColumnIndex(LATITUDE)));
                    attendanceCalendarMsgData.setLongitude(cursor.getString(cursor.getColumnIndex(LONGITUDE)));
                    attendanceCalendarMsgData.setPart(cursor.getString(cursor.getColumnIndex(PART)));
                    attendanceCalendarMsgData.setStatus(cursor.getString(cursor.getColumnIndex(STATUS)));
                    attendanceCalendarMsgData.setTime(cursor.getString(cursor.getColumnIndex(TIME)));
                    attendanceCalendarMsgData.setCheat(cursor.getString(cursor.getColumnIndex(CHEAT)));
                    attendanceCalendarMsgData.setCheat_device_id(cursor.getString(cursor.getColumnIndex(CHEAT_DEVICE_ID)));
                    attendanceCalendarMsgData.setDate(cursor.getString(cursor.getColumnIndex(DATE)));
                    attendanceCalendarMsgData.setGroup_id(cursor.getString(cursor.getColumnIndex(GROUP_ID)));
                    attendanceCalendarMsgData.setAtten_calendar_id(cursor.getString(cursor.getColumnIndex(ATTEN_CALENDAR_ID)));
                    attendanceCalendarMsgData.setApproval_end_part(cursor.getString(cursor.getColumnIndex(APPROVAL_END_PART)));
                    attendanceCalendarMsgData.setApproval_status(cursor.getString(cursor.getColumnIndex(APPROVAL_STATUS)));
                    attendanceCalendarMsgData.setApproval_reason(cursor.getString(cursor.getColumnIndex(APPROVAL_REASON)));
                    attendanceCalendarMsgData.setApproval_type(cursor.getString(cursor.getColumnIndex(APPROVAL_TYPE)));
                    attendanceCalendarMsgData.setApproval_done_time(cursor.getString(cursor.getColumnIndex(APPROVAL_DONE_TIME)));
                    attendanceCalendarMsgData.setApproval_address(cursor.getString(cursor.getColumnIndex(APPROVAL_ADDRESS)));
                    attendanceCalendarMsgData.setApproval_leave_type(cursor.getString(cursor.getColumnIndex(APPROVAL_LEAVE_TYPE)));
                    attendanceCalendarMsgData.setApproval_end_time(cursor.getString(cursor.getColumnIndex(APPROVAL_END_TIME)));
                    attendanceCalendarMsgData.setApprove_admin(cursor.getString(cursor.getColumnIndex(APPROVAL_ADMIN)));
                    attendanceCalendarMsgData.setApproval_day(cursor.getString(cursor.getColumnIndex(APPROVAL_DAY)));
                    attendanceCalendarMsgData.setApproval_start_time(cursor.getString(cursor.getColumnIndex(APPROVAL_START_TIME)));
                    attendanceCalendarMsgData.setApproval_time(cursor.getString(cursor.getColumnIndex(APPROVAL_TIME)));
                    attendanceCalendarMsgData.setApproval_start_part(cursor.getString(cursor.getColumnIndex(APPROVAL_START_PART)));
                    datas.add(attendanceCalendarMsgData);
                } else if ("-1".equals(cursor.getString(cursor.getColumnIndex(TYPE)))) {
                    LegworkRecordData legworkRecordData = new LegworkRecordData();
                    legworkRecordData.setId(cursor.getString(cursor.getColumnIndex(ID)));
                    legworkRecordData.setUserId(cursor.getString(cursor.getColumnIndex(USER_ID)));
                    legworkRecordData.setLatitude(cursor.getDouble(cursor.getColumnIndex(LATITUDE)));
                    legworkRecordData.setLongitude(cursor.getDouble(cursor.getColumnIndex(LONGITUDE)));
                    legworkRecordData.setAddress(cursor.getString(cursor.getColumnIndex(ADDRESS)));
                    legworkRecordData.setRemark(cursor.getString(cursor.getColumnIndex(REMARK)));
                    legworkRecordData.setTime(cursor.getString(cursor.getColumnIndex(TIME)));
                    legworkRecordData.setAttachment(cursor.getString(cursor.getColumnIndex(ATTACHMENT)));
                    datas.add(legworkRecordData);

                }

            }
        } catch (Exception e) {
            Log.d("attendance", "Exception" + e.toString());
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return datas;
    }


    /**
     * 删除某张表下所有消息
     *
     * @param date
     * @return
     */
    public synchronized int deleteAttendanceByDate(String date, String uid, String groupId) {
        createScheduleTable(date);
        return mDb.delete("_" + date, USER_ID + " = ? and " + GROUP_ID + " = ? ", new String[]{uid, groupId});
    }


}
